package dado;

import java.sql.*;
import java.util.ArrayList;
import organizador.User;

public class DAOUser extends DAODefault{
	
	public void createUser(User user){
		
		String sql = "INSERT INTO user"+
					  "(nome, sobrenome, email, telefone, cpf, logradouro, cidade, estado) "
				+"VALUES(?,?,?,?,?,?,?,?)";
		
		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, user.getNome());
			stmt.setString(2, user.getSobrenome());
			stmt.setString(3, user.getEmail());
			stmt.setString(4, user.getTelefone());
			stmt.setString(5, user.getCpf());
			stmt.setString(6, user.getLogradouro());
			stmt.setString(7, user.getCidade());
			stmt.setString(8, user.getEstado());
			
			stmt.execute();

			stmt.close();

			
		} catch (SQLException e){
			throw new RuntimeException(e);

		}
		
	}
	
	public ArrayList<User> readUser(){
		ArrayList<User> users = new ArrayList<>();
		String sql = "SELECT * FROM USER";
		
		try {
			stmt = conn.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				User user = new User();
				user.setId(rs.getInt("idUser"));
				user.setNome(rs.getString("nome"));
				user.setSobrenome(rs.getString("sobrenome"));
				user.setEmail(rs.getString("email"));
				user.setTelefone(rs.getString("telefone"));
				user.setCpf(rs.getString("cpf"));
				user.setLogradouro(rs.getString("logradouro"));
				user.setCidade(rs.getString("cidade"));
				user.setEstado(rs.getString("estado"));
				users.add(user);
			}
			return users;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

public ArrayList<User> searchUser(String dado){
		
		try{
			ArrayList<User> users = new ArrayList<>();

		
		String sql = "SELECT * FROM User WHERE NOME = ?  OR SOBRENOME=? OR EMAIL = ? OR" +
				" TELEFONE = ? OR CPF = ?";
		
		
			stmt = conn.prepareStatement(sql);
			

			stmt.setString(1, dado);
			stmt.setString(2, dado);
			stmt.setString(3, dado);
			stmt.setString(4, dado);
			stmt.setString(5, dado);
		
			
			ResultSet rs = stmt.executeQuery();
			
			while(rs.next()){
				
				User userACME = new User();
				
				userACME.setId(rs.getInt("idUser"));
				userACME.setNome(rs.getString("nome"));
				userACME.setSobrenome(rs.getString("sobrenome"));
				userACME.setCpf(rs.getString("cpf"));
				userACME.setCidade(rs.getString("cidade"));
				userACME.setEmail(rs.getString("email"));
				userACME.setTelefone(rs.getString("telefone"));
				userACME.setEstado(rs.getString("estado"));
				userACME.setLogradouro(rs.getString("logradouro"));
				
				users.add(userACME);
			}
			
			return users;
		}
		catch (SQLException e){
			throw new RuntimeException(e);
		}
	}


	public void deleteUser(User users){
		try{
			String sql = "DELETE FROM USER WHERE idUser=?";
			
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, users.getId());
			stmt.execute();
			stmt.close();
		}
		catch (SQLException e){
			throw new RuntimeException(e);
		}
	}
	
	public void updateUser(User user){
		
		String sql = "UPDATE USER SET " + 
				"nome=?, sobrenome=?, email=?, telefone=?, logradouro=?, cidade=?, estado=? "+
				"where cpf = ?"; 

					  
		
		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, user.getNome());
			stmt.setString(2, user.getSobrenome());
			stmt.setString(3, user.getEmail());
			stmt.setString(4, user.getTelefone());
			stmt.setString(5,user.getLogradouro());
			stmt.setString(6, user.getCidade());
			stmt.setString(7, user.getEstado());
			stmt.setString(8,user.getCpf());
			
			stmt.execute();

			stmt.close();

			
			
		} catch (SQLException e){
			throw new RuntimeException(e);

		}
		
	}
	


}
